﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.ComponentModel;
using System.Drawing.Design;
using BaseTypesLib.UiTypeEditors;
using System.Xml.Linq;

namespace BaseTypesLib.Model.Backgrounds
{
    [Editor(typeof(BackgroundEditor), typeof(UITypeEditor))]
    public abstract class Background: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            if(PropertyChanged!=null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        public virtual XElement GetXml()
        {
            XElement elem = new XElement(GetType().Name);
            return elem;
        }
        protected virtual void FromXmlElement(XElement elem) { }
        public static Background FromXml(XElement elem)
        {
            Background bg = GetFromName(elem.Name.LocalName);
            bg.FromXmlElement(elem);
            return bg;
        }

        private static Background GetFromName(string p)
        {
            if (p == "SolidColorBackground")
                return new SolidColorBackground();
            else if (p == "TransparentColorBackground")
                return new TransparentColorBackground();
            else if (p == "LinearGradientBackground")
                return new LinearGradientBackground();
            else if (p == "ImageBackground")
                return new ImageBackground();
            else throw new Exception(string.Format("Unknown background type '{0}'", p));
        }
    }
}
